<template>
	<view class="uni-tab-bar">
	
		<scroll-view id="tab-bar" class="uni-swiper-tab" scroll-x :scroll-left="scrollLeft">
			<view v-for="(tab,index) in tabBars" :key="tab.id" class="swiper-tab-list" :class="tabIndex==index ? 'active' : ''"
			 :id="tab.id" :data-current="index" @click="tapTab">{{tab.name}}</view>
		</scroll-view>
		
		<swiper :current="tabIndex" class="swiper-box" :duration="300" @change="changeTab">
			<swiper-item v-for="(tab,index1) in tabBars" :key="index1">
				<scroll-view class="list" scroll-y @scrolltolower="loadMore(index1)">
					<block v-for="(chapterItem,index2) in chapterItems[tab.id]" :key="index2">
						<media-list :options="chapterItem" @close="close(index1,index2)" @click="goExam(chapterItem)"></media-list>
					</block>
				</scroll-view>
			</swiper-item>
		</swiper>
		 
	</view>
</template>
<script>
	import mediaList from '@/components/tab-nvue/mediaList.vue';

	export default {
		components: {
			mediaList
		},
		data() {
			return {
				tabIndex: 0,
				tabBars: [],
				chapterItems: {} //map
			}
		},
		onLoad() {
			this.initChapterView();
		},
		methods: {
			initChapterView:function(){
				uni.request({
					url: this.websiteUrl+'/examdemo/chapter/initChapterView',
					method: 'GET',
					data: {},
					success: res => {
						console.log(res);
						this.tabBars = res.data.data.catalogList;
						this.chapterItems = res.data.data.charpterMap; //hashMap
					},
					fail: () => {},
					complete: () => {}
				});
			},
			
			async changeTab(e) {
				let index = e.target.current;
			
							
				if (this.isClickChange) {
					this.tabIndex = index;
					this.isClickChange = false;
					return;
				}
				let tabBar = await this.getElSize("tab-bar"),
					tabBarScrollLeft = tabBar.scrollLeft;
				let width = 0;
			
				for (let i = 0; i < index; i++) {
					let result = await this.getElSize(this.tabBars[i].id);
					width += result.width;
				}
				let winWidth = uni.getSystemInfoSync().windowWidth,
					nowElement = await this.getElSize(this.tabBars[index].id),
					nowWidth = nowElement.width;
				if (width + nowWidth - tabBarScrollLeft > winWidth) {
					this.scrollLeft = width + nowWidth - winWidth;
				}
				if (width < tabBarScrollLeft) {
					this.scrollLeft = width;
				}
				this.isClickChange = false;
				this.tabIndex = index; //一旦访问data就会出问题
			},
			getElSize(id) { //得到元素的size
				return new Promise((res, rej) => {
					uni.createSelectorQuery().select("#" + id).fields({
						size: true,
						scrollOffset: true
					}, (data) => {
						res(data);
					}).exec();
				})
			},
			async tapTab(e) { //点击tab-bar
				let tabIndex = e.target.dataset.current;
							
				if (this.tabIndex === tabIndex) {
					return false;
				} else {
					let tabBar = await this.getElSize("tab-bar"),
						tabBarScrollLeft = tabBar.scrollLeft; //点击的时候记录并设置scrollLeft
					this.scrollLeft = tabBarScrollLeft;
					this.isClickChange = true;
					this.tabIndex = tabIndex;
				}
			},
			
			goExam(e){
				//首先判断一下用户是否是登录状态，如果没有登录不能进入考试页面
				const value = uni.getStorageSync('loginUser');
				if (value) {
					//页面跳转到考试页面
					uni.navigateTo({
						url: '/pages/exam/exam?cid='+e.id
					});
				}else{
					uni.showToast({
					    icon: 'none',
					    title: '考试前请先登录！'
					});
				}
				
				
				
				
			}
		}
	}
</script>

<style>
	.uni-tab-bar-loading {
		text-align: center;
		font-size: 28upx;
		color: #999;
	}
</style>
